package com.demo.common;

import com.demo.common.controller.BlogController;
import com.demo.common.controller.IndexController;
import com.demo.common.controller.InterceptorController;
import com.demo.common.controller.ModelController;
import com.demo.common.controller.ParamController;
import com.demo.common.controller.RenderController;
import com.demo.common.controller.RendererrorController;
import com.demo.common.controller.RenderjsonController;
import com.demo.common.controller.TestController;
import com.demo.common.controller.testinterceptor.InterceptorStackTestController;
import com.demo.common.controller.testinterceptor.InterceptorTestController;
import com.demo.common.controller.testinterceptor.InterceptorTestTwoController;
import com.demo.common.model.Blog;
import com.demo.interceptor.GlobalInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

public class CommonConfig extends JFinalConfig {
	/**
	 * 配置JFinal的常量值
	 */
	@Override
	public void configConstant(Constants me) {
		//加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("config.properties");
		//设置当前开发模式 如果设置为true 控制台会输出每次请求的Controller action和参数信息
		me.setDevMode(getPropertyToBoolean("devMode", false));
		//设置视图类型
		me.setViewType(ViewType.JSP);
		//设置view层根目录
		me.setBaseViewPath("/WEB-INF/view");
		//设置http 404错误跳转页面
		me.setError404View("/WEB-INF/view/error/404.html");
		me.setError401View("/WEB-INF/view/error/404.html");
		me.setError403View("/WEB-INF/view/error/404.html");
		me.setError500View("/WEB-INF/view/error/404.html");
		me.setErrorView(400, "/WEB-INF/view/error/404.html");
		//设置文件下载地址
		me.setFileRenderPath("/download");
		//设置Freemarker模板引擎模板文件的后缀名
		me.setFreeMarkerViewExtension(".ftl");
		//设置国际化资源
		//me.setI18n("i18nResourceBaseName");
		//设置上传文件默认保存路径根目录
		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath() +"/upload");
		//设置URL参数分隔符 http://localhost/demo/blog/1-3  1为文章分类ID 3为分页页码 
		me.setUrlParaSeparator("-");
		
	}
	/**
	 * 配置路由映射
	 */
	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/blog", BlogController.class);
		me.add("/test", TestController.class);
		me.add("/param", ParamController.class);
		me.add("/render", RenderController.class);
		me.add("/renderjson", RenderjsonController.class);
		me.add("/rendererror", RendererrorController.class);
		me.add("/interceptor", InterceptorController.class);
		me.add("/inttest", InterceptorTestController.class);
		me.add("/inttest2", InterceptorTestTwoController.class);
		me.add("/intstatck", InterceptorStackTestController.class);
		me.add("/model", ModelController.class);
		
		//前后端路由按组分开
		//me.add(new AdminRoutes());
		//me.add(new WebRoutes());
		//me.add(new WeixinRoutes());
	}
	/**
	 * 配置插件
	 */
	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin dbPlugin = new C3p0Plugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(dbPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dbPlugin);
		arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
		arp.setShowSql(true);
		me.add(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
//		me.add(new GlobalOneInterceptor());
		me.add(new GlobalInterceptor());
	}
	/**
	 * 配置全局公共处理器
	 */
	@Override
	public void configHandler(Handlers me) {
//		me.add(new HtmlTwoHandler());
//		me.add(new HtmlHandler());
		
	}
	@Override
	public void afterJFinalStart() {
		System.out.println("JFinal启动后 启动自动调度线程 ");
	}
	@Override
	public void beforeJFinalStop() {
		System.out.println("调度线程释放");
		
	}
	
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
